<@Page title="Nowe wiadomości">
			
		<button value="Dodaj odbiorcę" id="addOdbiorce" onclick="addOdbiorceAction()" data-theme="a"></button>
		<form>
			<div data-role="fieldcontain" data-theme="a">
				
				<fieldset data-role="controlgroup" data-type="horizontal" id="fieldsetOdb" >
				<legend>Wyślij do:</legend>
				
					<label for="typOdb1">Typ:</label>
					<select name="messageReciversTyp1" id="typOdb1" data-mini="true" onchange="changeValue('typOdb1', 'nameOdb1')">
						<option value="x">Wybierz typ</option>
						<option value="1">Administrator</option>
						<option value="2">Instruktor</option>
						<option value="3">Kursant</option>
					</select>
					
					<label for="nameOdb1">Odbiorca:</label>
					<select name="messageRecivers1" id="nameOdb1" data-mini="true">
						<option value="x">Wybierz odbiorcę</option>
					</select>
					
					<br>
					<br>
					<div id="wiecejOdbiorcy"></div>
				</fieldset>
			
			</div>
			
			<div data-role="fieldcontain" data-theme="a">
				<label for="messageTopic">Temat:</label>
				<input type="text" id="messageTopic" name="messageTopic"></input>
			</div>
			<div data-role="fieldcontain" data-theme="a">
				<label for="messageContent">Treść:</label>
				<textarea name="messageContent" id="messageContent"></textarea>
			</div>
			
			<button value="Wyślij" onClick="sendAction()">Wyślij</button>
		</form>
		
		 <script>
			
			var iloscOdbiorcow = 1;
			
			function changeValue(typOdbSelect, nameOdbSelect){
				var newUrl;
				var typOdb = document.getElementById(typOdbSelect).value;
				
				if(typOdb == "1"){
					newUrl = "message!listAdministrator.action";
				}else if(typOdb == "2"){
					newUrl = "message!listInstruktor.action";
				}else if(typOdb == "3"){
					newUrl = "message!listKursant.action";
				}else{
					newUrl = "error";
				}
				
				$("#"+nameOdbSelect)
				    .find('option')
				    .remove()
				    .end();
				
				  $.ajax(
							   {
							      type: "POST",
							      url: newUrl,
							      dataType: "json",
							      success: function( response ) 
							      { 
							      	for(i=0; i<response.length; i++){
							    	  $("#"+nameOdbSelect).append('<option value="'+ response[i].id +'&'+ response[i].imie +'&'+ response[i].nazwisko +'&'+response[i].rodzaj+'" selected="selected">' + response[i].imie +" "+ response[i].nazwisko +'</option>').trigger('create');
							    	 }
							    	 
							    	 $("#"+nameOdbSelect).selectmenu('refresh');
							      }
							   } );
			}
			
			
			function addOdbiorceAction(){
				iloscOdbiorcow++;
				var onChangValue = "'typOdb" + iloscOdbiorcow +"', 'nameOdb" + iloscOdbiorcow + "' ";
				
				var stringSelect = '<fieldset data-role="controlgroup" data-type="horizontal" id="fieldsetOdb" >';
					stringSelect += '<br><label for="typOdb' + iloscOdbiorcow + '">Typ:</label>';
					stringSelect += '<select name="messageReciversTyp' + iloscOdbiorcow + '" id="typOdb' + iloscOdbiorcow + '" data-mini="true"  onchange="changeValue(' + onChangValue + ')">';
					stringSelect += '<option value="x">Wybierz typ</option>';
					stringSelect += '<option value="1">Administrator</option>';
					stringSelect += '<option value="2">Instruktor</option>';
					stringSelect += '<option value="3">Kursant</option>';
					stringSelect += '</select>';
					stringSelect += '<label for="nameOdb' + iloscOdbiorcow + '">Odbiorca:</label>';
					stringSelect += '<select name="messageRecivers' + iloscOdbiorcow + '" id="nameOdb' + iloscOdbiorcow + '" data-mini="true">';
					stringSelect += '<option value="x">Wybierz odbiorcę</option>';
					stringSelect += '</select></fieldset>';
					
				$('#wiecejOdbiorcy').append(stringSelect).trigger('create');
				$('#typOdb' + iloscOdbiorcow).selectmenu('refresh');
				$('#nameOdb' + iloscOdbiorcow).selectmenu('refresh');
			}
			
			
			function sendAction(){
				var selectedItems = "{odbiorcy: [";
							
							for (var i = 1; i < iloscOdbiorcow+1; i++) {
								var str =$("#nameOdb"+i).attr('value');
								var ret = str.split("&");
								var idOdb = ret[0];
								var imieOdb = ret[1];	
								var nazwiskoOdb = ret[2];	
								var rodzajOdb = ret[3];								
								
								if(idOdb != "x"){
									selectedItems += "{ ";
									selectedItems += "id:'" + idOdb + "', ";
									selectedItems += "imie:'" + imieOdb + "', ";
									selectedItems += "nazwisko:'" + nazwiskoOdb + "', ";
									selectedItems += "rodzaj:'" + rodzajOdb +"' }, ";
								}
							}
							selectedItems += "]}";	
							
				var queryObject = {
					"messageTopic" : messageTopic.value,
					"messageContent": messageContent.value,
					"messageRecivers": selectedItems
				};
				
				
				$.ajax({			
					type: "POST",
					url: "message!send.action",
					data: queryObject,
			        success: function( response )  {
			         	window.location.href="message!outMessage.action";
			        }
			        });
		 	 }
			
			
		 	
		</script>
		
</@Page>
